﻿Imports Helpers
Partial Public Class Login
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.Title = ConfigurationManager.AppSettings("sitename") & " Amministrazione - ACCESSO"
    End Sub
    Private Sub ctrlLogin_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles ctrlLogin.Authenticate
        Try
            Dim help As New Common
            If Not String.IsNullOrEmpty(ctrlLogin.UserName) And Not String.IsNullOrEmpty(ctrlLogin.Password) Then
                Dim utente As Utente = DAL.Context.Utente.Where(Function(u) u.Username = ctrlLogin.UserName _
                                                                         And u.Password = ctrlLogin.Password).Single
                If utente IsNot Nothing Then
                    Dim _remember As Boolean = ctrlLogin.RememberMeSet
                    Dim _role As String = utente.Ruolo.Descrizione
                    FormsAuthentication.Initialize()
                    Dim oTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, utente.Username, DateTime.Now, DateTime.Now.AddMinutes(60), _remember, _role, FormsAuthentication.FormsCookiePath)
                    Dim strHash As String = FormsAuthentication.Encrypt(oTicket)
                    Dim oCookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, strHash)
                    If (oTicket.IsPersistent) Then oCookie.Expires = oTicket.Expiration
                    Context.Response.Cookies.Add(oCookie)
                    Response.Redirect(Page.ResolveUrl(ctrlLogin.DestinationPageUrl))
                Else
                    e.Authenticated = False
                End If
            Else
                e.Authenticated = False
            End If
        Catch ex As Exception
            e.Authenticated = False
        End Try
    End Sub
End Class